﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Web.Mvc.Ajax;

namespace OAclient.Common
{
    public static class ComHelper
    {
        public static MvcHtmlString PageLinks(this AjaxHelper helper,
            PagingInfo paginInfo, Func<Int32, String> pageUrl, AjaxOptions ajaxOptions)
        {
            paginInfo.TotalItems = paginInfo.TotalItems == 0 ? 1 : paginInfo.TotalItems;
            String ajaxClick = "Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), " +
                "{ insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: '" + ajaxOptions.UpdateTargetId + "' });";
            StringBuilder result = new StringBuilder();

            var totalPage = new TagBuilder("div");
            totalPage.MergeAttribute("style", "float:left");
            totalPage.InnerHtml = "共" + paginInfo.TotalPages + "页，当前第" + paginInfo.CurrentPage + "页";
            result.AppendLine(totalPage.ToString());

            var firstPage = new TagBuilder("a");
            firstPage.InnerHtml = "首页";
            firstPage.MergeAttribute("href", pageUrl(1));
            firstPage.MergeAttribute("onclick", ajaxClick);
            result.AppendLine(firstPage.ToString());

            var prePage = new TagBuilder("a");
            if (paginInfo.CurrentPage > 1)
            {
                prePage.MergeAttribute("href", pageUrl((paginInfo.CurrentPage - 1)));
                prePage.MergeAttribute("onclick", ajaxClick);
            }
            prePage.InnerHtml = "上一页";
            result.AppendLine(prePage.ToString());
            //for (int pageIndex = 1; pageIndex <= paginInfo.TotalPages; pageIndex++)
            //{
            //    TagBuilder tag = new TagBuilder("a");
            //    if (pageIndex == paginInfo.CurrentPage)
            //        tag.AddCssClass("curr_page");
            //    tag.MergeAttribute("href", pageUrl(pageIndex));
            //    tag.InnerHtml = pageIndex.ToString();
            //    tag.MergeAttribute("onclick", ajaxClick);
            //    result.AppendLine(tag.ToString());
            //}
            result.AppendLine("&nbsp;第");
            var inputPage = new TagBuilder("input");
            inputPage.MergeAttribute("type", "text");
            inputPage.MergeAttribute("style", "width:30px");
            inputPage.MergeAttribute("onkeypress", "input_link(this,\'" + pageUrl(0) + "\',\'" + paginInfo.TotalPages + "\',\'" + ajaxOptions.UpdateTargetId + "\')");
            result.AppendLine(inputPage.ToString());
            result.AppendLine("页&nbsp;");

            var nextPage = new TagBuilder("a");
            if (paginInfo.CurrentPage < paginInfo.TotalPages)
            {
                nextPage.MergeAttribute("href", pageUrl((paginInfo.CurrentPage + 1)));
                nextPage.MergeAttribute("onclick", ajaxClick);
            }
            nextPage.InnerHtml = "下一页";
            result.AppendLine(nextPage.ToString());

            var lastPage = new TagBuilder("a");
            lastPage.MergeAttribute("href", pageUrl(paginInfo.TotalPages));
            lastPage.MergeAttribute("onclick", ajaxClick);
            lastPage.InnerHtml = "末页";
            result.AppendLine(lastPage.ToString());

            return MvcHtmlString.Create(result.ToString());
        }

        public static MvcHtmlString PageLinks(this HtmlHelper helper,
            PagingInfo pagingInfo, Func<Int32, String> pageUrl)
        {
            StringBuilder result = new StringBuilder();

            for (int pageIndex = 1; pageIndex <= pagingInfo.TotalPages; pageIndex++)
            {
                TagBuilder tag = new TagBuilder("a");
                tag.MergeAttribute("href", pageUrl(pageIndex));
                tag.InnerHtml = pageIndex.ToString();
                result.AppendLine(tag.ToString());
            }
            return MvcHtmlString.Create(result.ToString());
        }

        public static String ReadWord(String fileName)
        {
            try
            {
                Object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application oWord;
                Microsoft.Office.Interop.Word._Document oDoc;
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = false;
                Object file = fileName;
                oDoc = oWord.Documents.Open(ref file, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                var result = oDoc.Content.Text;

                oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                return result;
            }
            catch
            {
                return "Failed";
            }
        }

        public static String ConvertDayToOther(String day)
        {
            switch (day)
            {
                case "7":
                    return "一星期";
                case "21":
                    return "三星期";
                case "30":
                    return "一个月";
                case "60":
                    return "两个月";
                case "90":
                    return "三个月";
                default:
                    return "";
            }
        }

        public static String ConverOtherToDay(String day)
        {
            switch (day)
            {
                case "一星期":
                    return "7";
                case "三星期":
                    return "21";
                case "一个月":
                    return "30";
                case "两个月":
                    return "60";
                case "三个月":
                    return "90";
                default:
                    return "";
            }
        }
    }
}
